﻿Namespace Validation
    Public Class DateValidationRule
        Inherits WPFGlue.Validation.ValidationRule

        Public Property MinDate As Nullable(Of DateTime)
        Public Property MaxDate As Nullable(Of DateTime)
        Public Sub New()
            MyClass.New("Must be date between {1} and {2}.")
        End Sub

        Public Sub New(ByVal message As String)
            Me.ValidationStep = Controls.ValidationStep.RawProposedValue
            Me.Message = message
        End Sub

        Protected Overrides Function ValidateCore(ByVal value As Object, ByVal cultureInfo As System.Globalization.CultureInfo) As Boolean
            Dim dateValue As DateTime
            Dim result As Boolean = Date.TryParse(value, cultureInfo.DateTimeFormat.GetFormat(GetType(System.IFormatProvider)), System.Globalization.DateTimeStyles.None, dateValue)
            If result And MaxDate.HasValue Then
                result = (dateValue <= MaxDate)
            End If
            If result And MinDate.HasValue Then
                result = (dateValue >= MinDate)
            End If
            Return result
        End Function

        Protected Overrides Function FormatMessage(ByVal value As Object) As String
            Return String.Format(Me.Message, value, Me.MinDate, Me.MaxDate)
        End Function
    End Class
End Namespace
